<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="userForm">
    <input type="hidden" name="id">
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="loginName" id="loginName" lay-verify="required" lay-reqtext="用户名不能为空"
                       placeholder="请输入用户名" value="" class="layui-input">
                <tip>填写自己管理账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="name" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机</label>
            <div class="layui-input-block">
                <input type="number" name="phone" placeholder="请输入手机" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" name="email" placeholder="请输入邮箱" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">qq</label>
            <div class="layui-input-block">
                <input type="number" name="QQ" placeholder="请输入qq" value="" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="saveBtn">保存</button>
            </div>
        </div>
    </div>
</form>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //接受传来的 json 数据
        var jsonStr = window.rowData;
        //将 json 数据转换成 object 这时的 object 就是 user.html 的原 data 对象
        var data = JSON.parse(jsonStr)

        /* form.val("userForm", {
             'id':data.id,
             'loginName':data.loginName,
             'name':data.name,
             'phone':data.phone,
             'email':data.email,
             'qq':data.QQ
         });*/

        //直接这样写数据传输过来了 但是不是原来的 data 对象
        //user.html 和 user-edit.html 中 iframeWindow 的 window 对象不是同一个 各有个的内置对象
        //所以只能用 json 做一次转换 来保证传来的 data 是 user.html 中的原 data 对象
        form.val("userForm", data);


        //监听提交
        form.on('submit(saveBtn)', function (data) {

            $.post('../../user/update', data.field, function (data) {
                layer.msg('保存成功');
                /*保存成功关闭保存页面*/
                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

                parent.layui.table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                }, 'data');

                return false;
            }, 'json');

            return false;
        });

    });
</script>
</body>
</html>